<template>
  <el-upload
    action="#"
    :http-request="updateImage"
    list-type="picture-card"
    :file-list="fileList"
    :limit="limit"
  >
    <i class="el-icon-plus" />
  </el-upload>
</template>

<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDtd8NM5XjfLluoVzFH4hAQREo9G8mCOM6',
  SecretKey: 'YrwnQp0wfAQSFtKXT9ZjcFckcDgusa1C'
})
export default {
  props: {
    limit: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      fileList: [],
      imageUrl: ''
    }
  },
  methods: {
    // 上传图片
    updateImage(file) {
      const newFile = file.file
      cos.putObject({
        Bucket: 'hr-project-1311279921', /* 必须 */
        Region: 'ap-guangzhou', /* 存储桶所在地域，必须字段 */
        Key: newFile.name, /* 必须 */
        StorageClass: 'STANDARD',
        Body: newFile, // 上传文件对象
        onProgress: (progressData) => {
          console.log(JSON.stringify(progressData))
        }
      }, (err, data) => {
        if (err) {
          this.$message.error('上传文件失败!')
        } else {
          this.imageUrl = 'http://' + data.Location
          this.fileList.push({ url: 'http://' + data.Location, state: 'succeed' })
        }
      })
    }
  }
}
</script>

<style>

</style>
